﻿//--------------------------------------------------------------------------------------------------------------
// <copyright file="CSPCitizenMyCasesWUC.cs" company="Microsoft Corporation" author="David Martos" date="01/01/2008">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//--------------------------------------------------------------------------------------------------------------


using System;
using System.Collections;
using System.Data;
using System.Reflection;
using System.Resources;
using System.Runtime.InteropServices;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.Security;
using System.Xml.Serialization;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;


namespace CSP.WebParts.UserControls
{

    /// <summary>
    /// Shows the list of citizen cases
    /// </summary>    
    public class CSPCitizenMyCasesWUC : CSPBaseWUC
    {

        #region Members
        protected GridView gvCitizenMyCases;
        #endregion

        #region properties
        public int PageSize { get; set; }
        #endregion

        #region PageEvents
        /// <summary>
        /// Fired on page prerender
        /// </summary>  
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            try
            {
                LoadData();
            }
            catch (CSP.Core.CSPException cspex)
            {
                WrapperWebPart.AddError(LoadResource(WebPartConstants.CSPGenericErrorMessage));
            }
            catch (Exception ex)
            {
                this.LogMessage(this.LoadResource(WebPartConstants.CSPCitizenMyCasesLoadErrorMessage), this.GetType().Name, ex);
                WrapperWebPart.AddError(LoadResource(WebPartConstants.CSPGenericErrorMessage));
            }
        }

        /// <summary>
        /// Fired on page create child controls
        /// </summary>  
        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            try
            {
                gvCitizenMyCases.PageIndexChanging += new GridViewPageEventHandler(gvCitizenMyCases_PageIndexChanged);
                gvCitizenMyCases.Sorting += new GridViewSortEventHandler(gvCitizenMyCases_SortCommand);
                if (this.PageSize != 0)
                    gvCitizenMyCases.PageSize = this.PageSize;
            }
            catch (Exception ex)
            {
                this.LogMessage(LoadResource(WebPartConstants.CSPCitizenMyCasesCreateChildControlsErrorMessage), this.GetType().Name, ex);
                throw new CSP.Core.CSPException(ex);
            }
        }
        #endregion


        #region Methods
        /// <summary>
        /// Gets the list of a citizen cases
        /// </summary>
        protected void LoadData()
        {
            CSP.MembershipInterface.MembershipServiceClient client = null;
            try
            {
                string username = Context.User.Identity.Name;

                CSP.MembershipInterface.ObjectModel.Case[] cases = new CSP.MembershipInterface.ObjectModel.Case[0];


                client = new CSP.MembershipInterface.MembershipServiceClient();
                cases = client.GetCasesByCitizenID(username);
                client.Close();


                if (cases.Length == 0)
                {
                    this.gvCitizenMyCases.Visible = false;
                    WrapperWebPart.AddError(LoadResource(WebPartConstants.CSPEmptyListMessage));
                }
                else
                {
                    // Now, convert the cases to rows in a DataTable for display
                    DataTable casesTable = new DataTable();
                    casesTable.Columns.Add("Title", typeof(string));
                    casesTable.Columns.Add("Description", typeof(string));
                    casesTable.Columns.Add("CaseStatusTitle", typeof(string));
                    casesTable.Columns.Add("DueDate", typeof(DateTime));
                    casesTable.Columns.Add("CaseTypeTitle", typeof(string));

                    foreach (CSP.MembershipInterface.ObjectModel.Case crmCase in cases)
                    {
                        casesTable.Rows.Add(new object[] { crmCase.Title, crmCase.Description, crmCase.Status.CaseStatusTitle, crmCase.DueDate, crmCase.Type.CaseTypeTitle });
                    }

                    DataView dataView = casesTable.DefaultView;

                    if (ViewState["SortExp"] != null && ViewState["SortDir"] != null)
                    {
                        dataView.Sort = ViewState["SortExp"] + " " + ViewState["SortDir"];
                    }

                    gvCitizenMyCases.DataSource = dataView.ToTable();
                    gvCitizenMyCases.DataBind();
                }
            }
            catch (CSP.Core.CSPException cspex)
            {
                throw cspex;
            }
            catch (Exception ex)
            {
                this.LogMessage(this.LoadResource(WebPartConstants.CSPGenericErrorMessage), this.GetType().Name, ex);
                throw ex;
            }
            finally
            {
                if (client != null && client.State != System.ServiceModel.CommunicationState.Closed)
                    client.Close();
            }





        }
        #endregion


        #region ControlEvents
        /// <summary>
        /// Fired on gridview sorting
        /// </summary>  
        protected void gvCitizenMyCases_SortCommand(object sender, GridViewSortEventArgs e)
        {
            try
            {
                base.GridViewSortCommand(((GridView)sender), e);
            }
            catch (Exception ex)
            {
                this.LogMessage(this.LoadResource(WebPartConstants.CSPCitizenMyCasesSortingErrorMessage), this.GetType().Name, ex);
                throw new CSP.Core.CSPException(ex);
            }
        }

        /// <summary>
        /// Fired on gridview paging
        /// </summary>  
        protected void gvCitizenMyCases_PageIndexChanged(object sender, GridViewPageEventArgs e)
        {
            try
            {
                base.GridViewPageChangedCommand(((GridView)sender), e);
            }
            catch (Exception ex)
            {
                this.LogMessage(this.LoadResource(WebPartConstants.CSPCitizenMyCasesPagingErrorMessage), this.GetType().Name, ex);
                throw new CSP.Core.CSPException(ex);
            }
        }
        #endregion

    }
}
